﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using Osobe;
using SkolskaUstanova;
using MolbeIPotvrde;

namespace ElektronskiDnevnik
{
    public partial class VerifikacijaN : Form
    {
        DateTime datum;
        int odabraniPredmet, ocjena;
        String odabraniUcenik, JMBG, tipProvjere;
        MySqlConnection conn;
        MySqlDataReader rdr = null;
        public VerifikacijaN(String maticni, MySqlConnection c, int predmet, String ucenik, int oc, DateTime dat, String tip)
        {
            InitializeComponent();
            conn = c;
            odabraniPredmet = predmet;
            odabraniUcenik = ucenik;
            ocjena = oc;
            datum = dat;
            JMBG = maticni;
            tipProvjere = tip;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            String pass = "";
            MySqlCommand cmd = new MySqlCommand("SELECT * FROM Nastavnik WHERE JMBG = '" + JMBG + "';", conn);
            try
            {
                rdr = cmd.ExecuteReader();
                while (rdr.Read())
                    pass = Convert.ToString(rdr["Password"]);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Greska", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if (rdr != null)
                    rdr.Close();
            }
            if (textBox1.Text == pass)
            {
                MySqlCommand cmd2;
                MySqlCommand cmd1 = new MySqlCommand("SELECT * FROM UcenikPredmet WHERE Ucenik = '" + odabraniUcenik + "' AND Predmet = " + odabraniPredmet + ";", conn);
                try
                {
                    rdr = cmd1.ExecuteReader();
                    int ID_up = 0;
                    while (rdr.Read())
                        ID_up = Convert.ToInt32(rdr["ID"]);
                    rdr.Close();
                    cmd2 = new MySqlCommand("INSERT INTO Ocjene (" + "Datum, Ocjena, TipProvjere, ID_up" + ") VALUES('" + Convert.ToString(datum.Year) + "-" + Convert.ToString(datum.Month) + "-" + Convert.ToString(datum.Day) + "', " + ocjena + ", '" + tipProvjere + "', " + ID_up + ");", conn);
                    cmd2.ExecuteNonQuery();
                    this.pictureBox1.Image = global::ElektronskiDnevnik.Properties.Resources.images;
                    MessageBox.Show("Ocjena je upisana", "Obavijest", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Greska", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    if (rdr != null)
                        rdr.Close();
                    this.Close();
                }
            }
            else
            {
                this.pictureBox1.Image = global::ElektronskiDnevnik.Properties.Resources.smiley_angry;
                MessageBox.Show("Pogresan password", "Obavijest", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}
